package handler

import (
	"cinema-manager/model"
	"github.com/gin-gonic/gin"
	"strconv"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	movie := model.Movie{
		Title:   title,
		Content: content,
	}
	model.DB.Create(&movie)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func List(c *gin.Context) {
	var movie []model.Movie
	model.DB.Find(&movie)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": movie,
	})
}
func ConnectList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1*size)
	var data []model.MovieConnect
	model.DB.
		Table("connects").
		Select("movie_connects.title as Title",
			"movie_connects.content as Connect",
			"connects.id as Cid",
			"connects.text as Text").
		Joins("left join movie_connects").
		Limit(size).
		Offset(offset).
		Find(&data)
	var total int64
	model.DB.Table("connects").Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  data,
		"total": total,
	})
}
